package com.java.comprehensive.arithmetic.leecode.tags.array;

import cn.hutool.core.util.ArrayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: 窦绍飞
 * @Date: 2022/7/30
 */
public class LeetCode_39 {
	static List<List<Integer>> result = new ArrayList<>();

	List<Integer> list = new ArrayList<>();

	public List<List<Integer>> combinationSum(int[] candidates, int target) {
		Arrays.sort(candidates);
		helper(candidates, 0, target);
		return result;
	}

	public void helper(int[] candidates, int start, int target) {
		if (target == 0) {
			result.add(new ArrayList<>(list));
			return;
		}

		for (int i = start; i < candidates.length; i++) {
			if (candidates[i] > target) {
				break;
			}
			list.add(candidates[i]);
			helper(candidates, i, target - candidates[i]);
			list.remove(list.size() - 1);
		}
	}

	public static void main(String[] args) {
		int[] nums = {2, 3, 6, 7};
		LeetCode_39 methad = new LeetCode_39();
		methad.combinationSum(nums, 7);
		System.out.println(ArrayUtil.toString(result));
	}
}
